import com.mdxq.mall.product.ProductApp;
import com.mdxq.mall.product.entity.Brand;
import com.mdxq.mall.product.mapper.BrandMapper;
import com.mdxq.mall.product.mapper.ProductMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

@SpringBootTest(classes = ProductApp.class)
public class RedisTemplateDemo {
    @Autowired

    RedisTemplate redisTemplate;

    @Autowired
    BrandMapper mapper;

    @Test
    void t0(){
        Brand brand=mapper.selectOneById(3);
        ValueOperations<String,Brand> operations=redisTemplate.opsForValue();
        operations.set("brand:3",brand);
        redisTemplate.expire("brand:3",5, TimeUnit.MINUTES);
    }
    @Test
    void t1(){
        ValueOperations<String,Brand>operations=redisTemplate.opsForValue();
        System.out.println(operations.get("brand:3"));
    }
    @Test
    void t2(){
        List<Brand>brands=mapper.selectAll();
        ListOperations<String,Brand> operations=redisTemplate.opsForList();
        String key="allBrandList";
        if(redisTemplate.hasKey(key)){
            redisTemplate.delete(key);
        }
        // 将整个品牌列表全部 push 到Redis的列表中 ,返回push后列表的最终长度
        long llen=operations.leftPushAll(key,brands);
        if(llen>0){
            System.out.println("插入成功");
        }
    }
    @Test
    void t3(){
        ListOperations<String,Brand>operations=redisTemplate.opsForList();
        List<Brand> brands;
        if(redisTemplate.hasKey("allBrandList")){
            brands=operations.range("allBrandList",0,-1);
            System.out.println("缓存中获取");
        }else{
            brands=mapper.selectAll();
            operations.rightPushAll("allBrandList",brands);
            System.out.println("数据库中获取");
        }
        brands.forEach(System.out::println);
    }
    @Test
    void t4(){
        SetOperations<String,String>operations=redisTemplate.opsForSet();
        operations.add("水果1","苹果","香蕉","葡萄","沙糖桔");
        operations.add("水果2","橙子","香蕉","西瓜");
        System.out.println("交集");
        operations.intersect("水果1","水果2").forEach(System.out::println);
        System.out.println("并集");
        operations.union("水果1","水果2").forEach(System.out::println);
        System.out.println("差集");
        operations.difference("水果1","水果2").forEach(System.out::println);
    }
    @Test
    void t5(){
        ZSetOperations<String,String> operations=redisTemplate.opsForZSet();
        operations.add("水果","苹果",6.99);
        operations.add("水果","香蕉",3.99);
        operations.add("水果","梨",5.99);
        operations.add("水果","葡萄",4.99);
        operations.add("水果","西瓜",0.99);
        Set<ZSetOperations.TypedTuple<String>>zset=operations.rangeWithScores("水果",0,-1);
        zset.forEach(tuple->{
            System.out.println(tuple.getValue()+"  "+tuple.getScore());
        });
    }
    @Autowired
    ProductMapper productMapper;
    @Test
    void t6(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select("id","name","product_sn","price","brand_name");
        queryWrapper.eq("id",38);
        Map<String,Object>map=productMapper.selectOneByQueryAs(queryWrapper, HashMap.class);
        HashOperations<String,String,Object>hashOperations=redisTemplate.opsForHash();
        hashOperations.putAll("product:38",map);
    }
}
